package com.bookshop.ui;

import javax.swing.JPanel;

import com.bookshop.ui.admin.AdminHomePage;
import com.bookshop.ui.admin.AdminNavigater;
import com.bookshop.ui.component.AbstractNavigator;
import com.bookshop.ui.customer.CustomerHomePage;
import com.bookshop.ui.customer.CustomerNavigater;
import com.bookshop.ui.generalManager.GeneralManagerNavigater;
import com.bookshop.ui.generalManager.PromotionSettingPage;
import com.bookshop.ui.marketManager.CustomerManagePage;
import com.bookshop.ui.marketManager.MarketManagerNavigater;

public class PageJumper implements IJumper{	
	private static String[] sNavigater = {AdminNavigater.class.getSimpleName(),
										 GeneralManagerNavigater.class.getSimpleName(),
										 MarketManagerNavigater.class.getSimpleName(),
										 CustomerNavigater.class.getSimpleName()};
	private final JPanel mBasePanel;
	private AbstractNavigator mNavigator;
	private JPanel mPage;
	private String mAccount;
	
	public PageJumper(JPanel pBasePanel) {
		mBasePanel = pBasePanel;
	}
	
	public void loadHomePage(String pAccount, String pNavigaterName) {
		mAccount = pAccount;
		if (pNavigaterName.equals(sNavigater[0])) {
			
			setNav(new AdminNavigater(this, mAccount));
			loadPage(new AdminHomePage(this));
			
		} else if (pNavigaterName.equals(sNavigater[1])) {
	
			setNav(new GeneralManagerNavigater(this, mAccount));
			loadPage(new PromotionSettingPage(this));
			
		} else if (pNavigaterName.equals(sNavigater[2])) {
			
			setNav(new MarketManagerNavigater(this, mAccount));
			loadPage(new CustomerManagePage(this));
		} else if (pNavigaterName.equals(sNavigater[3])) {
			
			setNav(new CustomerNavigater(this, mAccount));
			loadPage(new CustomerHomePage(this));

		} else {
			throw new IllegalArgumentException("in pageJumper loadHomePage");
		}
	}
	
	public void setNav(AbstractNavigator pPanel) {
		mBasePanel.removeAll();
		mNavigator = pPanel;
		if (mNavigator != null) {
			mBasePanel.add(pPanel);
		}
		refresh(mBasePanel);
	}

	@Override
	public void loadPage(JPanel pPanel) {
		if (pPanel == null || pPanel == mPage) return;
		
		if (mPage != null) {
			mBasePanel.remove(mPage);
		}
		mPage = pPanel;
		mBasePanel.add(pPanel);
		refresh(mBasePanel);
	}
	
	public String getAccount() {
		return mAccount;
	}
	
	private static void refresh(JPanel pPanel) {
		pPanel.validate();
		pPanel.repaint();
	}
}

